package 位运算;

public class 只出现一次的数字II9 {
    //https://leetcode.cn/problems/single-number-ii/description/
    public int singleNumber1(int[] nums) {
        int ret = 0;
        int[] sum = new int[32];
        for (int i = 0; i < 32; i++)
            for (int num : nums)
                sum[i] += (num >> i) & 1;
        for (int i = 0; i < 32; i++)
            if(sum[i] % 3 == 1) ret |= (1 << i);
        return ret;
    }
    public int singleNumber(int[] nums) {
        int ret = 0;
        for (int i = 0; i < 32; i++) {
            int sum = 0;
            for (int x : nums)
                if (((x >> i & 1) == 1))
                    sum++;

            if(sum % 3 == 1)
                ret |= 1 << i;
        }return ret;
    }

}
